//
//  FMPlayerView.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/21.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FMPlayerView.h"
#import "FMPersonModel.h"
@implementation FMPlayerView
{
    UIWebView *webView;
}




-(instancetype)initWithFrame:(CGRect)frame array:(NSMutableArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews:array];
    }
    return self;
}
-(void)createSubviews:(NSMutableArray *)array
{
    
    self.array = [NSMutableArray arrayWithArray:array];
    
    
    self.Audio = [NSMutableArray arrayWithArray:array];
    
    self.backgroundColor = [UIColor blackColor];
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    [self addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(0, 800);
    self.scrollView.backgroundColor = [UIColor blackColor];
    [self.scrollView release];
   
    
    //背景图片
    self.MyImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH, 500 * HEIGHT)];
    [self.scrollView addSubview:self.MyImageView];
    [_MyImageView release];
    _MyImageView.backgroundColor = [UIColor blackColor];
    _MyImageView.userInteractionEnabled = YES;//打开用户交互
    
    
    //图片毛玻璃效果
    UIVisualEffectView *visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    //visualEfView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
    visualEfView.frame = self.MyImageView.frame;
    visualEfView.alpha = 1.0;
    [_MyImageView addSubview:visualEfView];
    
    
    
    
    
    UIView *view = [[UIView alloc]initWithFrame:_MyImageView.bounds];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.05;
    [_MyImageView addSubview:view];
    [view release];
    
    
    
    //旋转图片
    _image = [[UIImageView alloc]initWithFrame:CGRectMake(90 * WIDTH, 150 * HEIGHT, 200 * WIDTH, 200 * WIDTH)];
    _image.backgroundColor = [UIColor clearColor];
    [_MyImageView addSubview:_image];
    [_image release];
    _image.layer.masksToBounds = YES;//设置圆角边缘
    _image.layer.cornerRadius = _image.frame.size.height / 2;//设置弧度
    
    _image.center = CGPointMake(190 * WIDTH  ,200 * WIDTH  );
    
    
    
    
    
    
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(165 * WIDTH, 360 * HEIGHT , 50 * WIDTH , HEIGHT * 50);
    _button.backgroundColor = [UIColor clearColor];
    [_button setBackgroundImage:[UIImage imageNamed:@"toolbar_pause_n@2x.png"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [_MyImageView addSubview:_button];
    
    
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(85 * WIDTH,363 * HEIGHT, 50 * WIDTH, 50 * HEIGHT);
    [_MyImageView addSubview:leftButton];
    leftButton.backgroundColor = [UIColor clearColor];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"toolbar_prev_n@2x.png"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(240 * WIDTH ,365 * HEIGHT, 43 * WIDTH, 43 * HEIGHT);
    [_MyImageView addSubview:rightButton];
    rightButton.backgroundColor = [UIColor clearColor];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"toolbar_next_n@2x.png"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    _likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _likeButton.frame = CGRectMake(310 * WIDTH ,367 * HEIGHT, 35 * WIDTH, 35 * HEIGHT);
    [_MyImageView addSubview:_likeButton];
    _likeButton.backgroundColor = [UIColor clearColor];
  //  [_likeButton setBackgroundImage:[UIImage imageNamed:@"circlelist_heart@2x.png"] forState:UIControlStateNormal];
    [_likeButton addTarget:self action:@selector(likeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    
    //标签
    self.tagsTitle = [[UILabel alloc]initWithFrame:CGRectMake(20 * WIDTH ,530 * HEIGHT, 100 * WIDTH, 30 * HEIGHT)];
    _tagsTitle.text = @"标签:";
    _tagsTitle.textColor = [UIColor colorWithHexString:@"CCCCCC"];
    _tagsTitle.font = [UIFont systemFontOfSize:18];
    [self.scrollView addSubview:_tagsTitle];
    [_tagsTitle release];
    
    

    
    
    //标签内容
    self.tags = [[UILabel alloc]initWithFrame:CGRectMake(80 * WIDTH,515 * HEIGHT, 280 * WIDTH, 60 * HEIGHT)];
    [self.scrollView addSubview:self.tags];
    self.tags.numberOfLines = 0;
     _tags.font = [UIFont systemFontOfSize:17];
    self.tags.text = @"标签内容";
    self.tags.textColor = [UIColor colorWithHexString:@"CCCCCC"];
    [_tags release];

    
    
     self.scrollView.contentSize = CGSizeMake(0, self.tags.frame.origin.y + 80);
    

    //设置BOOL初始值为NO
    _isPlaying = YES;
    _isRotating = YES;
    _isCollection = NO;
    [self spinOn];
    
    
    
    
    
    
    
}





-(void)likeButtonAction
{
    
    if ([DataHandle shareHandleController].monitorNetwork == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力收藏不了呀" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
       
        return;
    }
    
    if (_isCollection == NO) {
        [_likeButton setBackgroundImage:[UIImage imageNamed:@"circlelist_heartup@2x.png"] forState:UIControlStateNormal];
      
        [[DataHandle shareHandleController]openDB];
        [[DataHandle shareHandleController]createMusicFMCollection];
        [self.myDelegate collection:YES];
         NSLog(@"lllllllllll");
        
        _isCollection = YES;
    }else{
    
        [_likeButton setBackgroundImage:[UIImage imageNamed:@"circlelist_heart@2x.png"] forState:UIControlStateNormal];
         [[DataHandle shareHandleController]openDB];
        NSLog(@"lalalalalalalal");
        [self.myDelegate drop:NO];
        _isCollection = NO;
    }
    
    
    
}




-(void)setAudio:(NSMutableArray *)Audio
{
    if (_Audio != Audio) {
        [_Audio release];
        _Audio = [Audio retain];
    }
   
}



-(void)buttonAction
{
    
    
    
 
    if (_isPlaying == YES && _isRotating == YES) {
        
         [self suspend];
        
        _isPlaying = YES;
        _isRotating = NO;
        [_button setBackgroundImage:[UIImage imageNamed:@"toolbar_play_n@2x.png"] forState:UIControlStateNormal];
        
        [_audioPlayer pause];//暂停
        
    }else if(_isPlaying == YES && _isRotating == NO)
    {
    
        [self ContinueToRotate];//继续旋转
        _isPlaying = YES;
        _isRotating = YES;
        [_button setBackgroundImage:[UIImage imageNamed:@"toolbar_pause_n@2x.png"] forState:UIControlStateNormal];
        [_audioPlayer resume];//继续播放
        
        
        
    }    
   
   
    
}



#pragma mark -- 设置图片旋转
-(void)spinOn//开始旋转
{
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//设置Z轴
    [basicAnimation setFromValue:[NSNumber numberWithInt:0]];//设置动画初始值
    [basicAnimation setToValue:[NSNumber numberWithInt:M_PI * 2]];//设置动画结束
    [basicAnimation setDuration:10];//设置动画时长
    [basicAnimation setRepeatCount:FLT_MAX];//无限旋转
    [basicAnimation setAutoreverses:NO];//动画结束后是否回到原来状态
    [basicAnimation setCumulative:YES];//累加 追加动画,不会出现结束后卡顿的现象,在上一次结束的地方继续开始
    [self.image.layer addAnimation:basicAnimation forKey:@"basicA"];
}

#pragma mark -- 图片旋转暂停
-(void)suspend//旋转暂停
{
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         
                     }
                     completion:^(BOOL finished){
                         if (finished){
                             [UIView animateWithDuration:1.0 animations:^{
                                 //pause
                                 CFTimeInterval pausedTime = [_image.layer convertTime:CACurrentMediaTime() fromLayer:nil];
                                 //时间停止
                                 _image.layer.speed = 0.0;
                                 //记录你停止的时间
                                 _image.layer.timeOffset = pausedTime;
                             }];
                         }
                     }
     ];
}

#pragma mark -- 图片继续旋转
-(void)ContinueToRotate
{
 
    //停止的时间
    CFTimeInterval pausedTime = [self.image.layer timeOffset];
  
    self.image.layer.speed = 1.0;
    self.image.layer.timeOffset = 0.0;
    self.image.layer.beginTime = 0.0;
    //用一共的时间 减去停止的时间
    CFTimeInterval timeSincePause = [self.image.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    //是暂停过后开始的时间
    self.image.layer.beginTime = timeSincePause;

}

-(void)Recovery
{
    
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//设置Z轴
    [basicAnimation setFromValue:[NSNumber numberWithInt:0]];//设置动画初始值
     [self.image.layer addAnimation:basicAnimation forKey:@"basicA"];
    
}

#pragma mark -- 左边按钮  跳到上一首
-(void)leftButtonAction
{
    [self.myDelegate lastSong];
}

#pragma mark -- 右边按钮  跳到下一首
-(void)rightButtonAction
{
    [self.myDelegate NextMusic];
    
}


-(void)setModel:(FMPlayerModel *)model
{
    
    
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    self.tagsTitle.text = @"标签:";
    
    //将url转成image
    
     NSURL *imageUrl = [NSURL URLWithString:[_model.images objectAtIndex:0]];
    
    //创建系统的全局队列(子线程)
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData *data = [NSData dataWithContentsOfURL:imageUrl];//单线程  阻塞线程
        dispatch_async(dispatch_get_main_queue(), ^{//回到主线程
            
            UIImage *image = [UIImage imageWithData:data];
             [self.myDelegate background:image];
    });
    });
    
    [_MyImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"cell_bg_noData_1@2x.png"]];

    [_image sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"cell_bg_noData_1@2x.png"]];
    
    
    
    
    //标签高度
    CGFloat tagsFloat = [self heightFor:_model.tags];
    self.tags.numberOfLines = 0;
    self.tags.text = _model.tags;
    if (_model.tags.length == 0) {

        self.tagsTitle.text = @" ";
    }


    
}

-(CGFloat)heightFor:(NSString *)content
{
    CGSize size = CGSizeMake(80, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17.0] forKey:NSFontAttributeName];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
    
}

@end
